package com.szpx.controller.daimingdongController;

import com.szpx.entity.daimingdongentity.*;
import com.szpx.service.StatementService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
public class StatementController {
    @Autowired
    private StatementService statementService;

    private static final Logger logger = LoggerFactory.getLogger(StatementController.class);

    @GetMapping("/toStatement")
    public String showReportHomePage() {
        return "lishihuiTeacherHtml/daimingdongAjaxHtml/home_Ajax"; // 返回报表统计首页的页面名称
    }


    @GetMapping("/tostock_in_report")
    public String showStockInReportPage(Model model) {
        //获取所有进货站报表
        List<StockInReport> stockInReports = statementService.getAllStockInReports();
        //将进货站报表添加到model中
        model.addAttribute("stockInReports", stockInReports);
        //打印进货站报表
        logger.info("StockInReports: {}", stockInReports);
        //返回进货站报表页面
        return "lishihuiTeacherHtml/daimingdongAjaxHtml/stock_in_report";//返回进货站报表
    }

    @GetMapping("/tostock_out_report")
    //显示进货站报表页面
    public String showStockOutReportPage(Model model) {
        //获取所有进货站报表
        List<StockOutReport> stockOutReports = statementService.getAllStockOutReports();
        //将进货站报表添加到model中
        model.addAttribute("stockOutReports", stockOutReports);
        //日志输出进货站报表
        logger.info("StockOutReports: {}", stockOutReports);
        //返回进货站报表页面
        return "lishihuiTeacherHtml/daimingdongAjaxHtml/stock_out_report";//返回进货站报表
    }

    @GetMapping("/tointernal_report")
    public String showInternalReportPage(Model model) {
        // 获取所有内部报表
        List<InternalReport> internalReports = statementService.getAllInternalReports();
        // 将内部报表添加到model中
        model.addAttribute("internalReports", internalReports);
        // 打印内部报表
        logger.info("InternalReports: {}", internalReports);
        // 返回内部报表页面的页面名称
        return "lishihuiTeacherHtml/daimingdongAjaxHtml/internal_report";
    }

    @GetMapping("/toreceipt_statistics")
    public String showReceiptStatisticsPage(Model model) {
        // 获取所有的收件统计信息
        List<ReceiptStatistics> receiptStatisticsList = statementService.getAllReceiptStatistics();
        // 将收件统计信息添加到model中
        model.addAttribute("receiptStatisticsList", receiptStatisticsList);
        // 打印收件统计信息
        logger.info("ReceiptStatisticsList: {}", receiptStatisticsList);
        // 返回收件统计页面的页面名称
        return "lishihuiTeacherHtml/daimingdongAjaxHtml/receipt_statistics";
    }

    @GetMapping("/todispatch_statistics")
    // 获取派件统计页面的方法
    public String showDispatchStatisticsPage(Model model) {
        // 获取所有的派件统计信息
        List<DispatchStatistics> dispatchStatisticsList = statementService.getAllDispatchStatistics();
        // 将派件统计信息添加到model中
        model.addAttribute("dispatchStatisticsList", dispatchStatisticsList);
        // 日志输出派件统计信息
        logger.info("DispatchStatisticsList: {}", dispatchStatisticsList);
        // 返回派件统计页面的页面名称
        return "lishihuiTeacherHtml/daimingdongAjaxHtml/dispatch_statistics";
    }

    @GetMapping("/search/stock_in_report")
    // 根据关键字搜索库存报表
    public String searchStockInReport(@RequestParam("keyword") String keyword, Model model) {
        // 调用statementService的searchStockInReports方法，根据关键字查询库存报表
        List<StockInReport> stockInReports = statementService.searchStockInReports(keyword);
        // 将查询到的库存报表添加到model中
        model.addAttribute("stockInReports", stockInReports);
        // 返回库存报表页面
        return "lishihuiTeacherHtml/daimingdongAjaxHtml/stock_in_report";
    }

    @GetMapping("/search/stock_out_report")
    // 根据关键字搜索库存报表
    public String searchStockOutReport(@RequestParam("keyword") String keyword, Model model) {
        // 调用statementService的searchStockOutReports方法，根据关键字查询库存报表
        List<StockOutReport> stockOutReports = statementService.searchStockOutReports(keyword);
        // 将查询到的库存报表添加到model中
        model.addAttribute("stockOutReports", stockOutReports);
        // 返回库存报表页面
        return "lishihuiTeacherHtml/daimingdongAjaxHtml/stock_out_report";
    }

    @GetMapping("/search/internal_report")
    // 根据关键字搜索内部报表
    public String searchInternalReport(@RequestParam("keyword") String keyword, Model model) {
        // 调用statementService的searchInternalReports方法，根据关键字查询内部报表
        List<InternalReport> internalReports = statementService.searchInternalReports(keyword);
        // 将查询到的内部报表添加到model中
        model.addAttribute("internalReports", internalReports);
        // 返回内部报表页面
        return "lishihuiTeacherHtml/daimingdongAjaxHtml/internal_report";
    }


    @GetMapping("/search/receipt_statistics")
    // 根据关键字搜索收货统计
    public String searchReceiptStatistics(@RequestParam("keyword") String keyword, Model model) {
        // 调用statementService的searchReceiptStatistics方法，根据关键字查询收货统计
        List<ReceiptStatistics> receiptStatisticsList = statementService.searchReceiptStatistics(keyword);
        // 将查询到的收货统计添加到model中
        model.addAttribute("receiptStatisticsList", receiptStatisticsList);
        // 返回收货统计页面
        return "lishihuiTeacherHtml/daimingdongAjaxHtml/receipt_statistics";
    }

    @GetMapping("/search/dispatch_statistics")
    // 根据关键字搜索发货统计
    public String searchDispatchStatistics(@RequestParam("keyword") String keyword, Model model) {
        // 调用statementService的searchDispatchStatistics方法，根据关键字查询发货统计
        List<DispatchStatistics> dispatchStatisticsList = statementService.searchDispatchStatistics(keyword);
        // 将查询到的发货统计添加到model中
        model.addAttribute("dispatchStatisticsList", dispatchStatisticsList);
        // 返回发货统计页面
        return "lishihuiTeacherHtml/daimingdongAjaxHtml/dispatch_statistics";
    }



    @GetMapping("/export/stock_in_report")
    public void exportStockInReport(HttpServletResponse response) throws IOException {
        List<StockInReport> stockInReports = statementService.getAllStockInReports();

        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Stock In Report");

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("序号");
        headerRow.createCell(1).setCellValue("站点名称");
        headerRow.createCell(2).setCellValue("货物名称");
        headerRow.createCell(3).setCellValue("数量");
        headerRow.createCell(4).setCellValue("总金额");
        headerRow.createCell(5).setCellValue("报表日期");

        // 填充数据行
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int rowNum = 1;
        for (StockInReport report : stockInReports) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(report.getId());
            row.createCell(1).setCellValue(report.getStationName());
            row.createCell(2).setCellValue(report.getGoodsName());
            row.createCell(3).setCellValue(report.getQuantity());
            row.createCell(4).setCellValue(report.getTotalAmount().doubleValue());
            row.createCell(5).setCellValue(formatter.format(report.getReportDate()));
        }

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=stock_in_report.xlsx");

        // 写入响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    @GetMapping("/export/stock_out_report")
    public void exportStockOutReport(HttpServletResponse response) throws IOException {
        List<StockOutReport> stockOutReports = statementService.getAllStockOutReports();

        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Stock Out Report");

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("序号");
        headerRow.createCell(1).setCellValue("站点名称");
        headerRow.createCell(2).setCellValue("货物名称");
        headerRow.createCell(3).setCellValue("数量");
        headerRow.createCell(4).setCellValue("总金额");
        headerRow.createCell(5).setCellValue("报表日期");

        // 填充数据行
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int rowNum = 1;
        for (StockOutReport report : stockOutReports) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(report.getId());
            row.createCell(1).setCellValue(report.getStationName());
            row.createCell(2).setCellValue(report.getGoodsName());
            row.createCell(3).setCellValue(report.getQuantity());
            row.createCell(4).setCellValue(report.getTotalAmount().doubleValue());
            row.createCell(5).setCellValue(formatter.format(report.getReportDate()));
        }

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=stock_out_report.xlsx");

        // 写入响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    @GetMapping("/export/internal_report")
    public void exportInternalReport(HttpServletResponse response) throws IOException {
        List<InternalReport> internalReports = statementService.getAllInternalReports();

        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Internal Report");

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("序号");
        headerRow.createCell(1).setCellValue("站点名称");
        headerRow.createCell(2).setCellValue("操作类型");
        headerRow.createCell(3).setCellValue("数量");
        headerRow.createCell(4).setCellValue("报表日期");

        // 填充数据行
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int rowNum = 1;
        for (InternalReport report : internalReports) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(report.getId());
            row.createCell(1).setCellValue(report.getStationName());
            row.createCell(2).setCellValue(report.getOperationType());
            row.createCell(3).setCellValue(report.getQuantity());
            row.createCell(4).setCellValue(formatter.format(report.getReportDate()));
        }

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=internal_report");

        // 写入响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    @GetMapping("/export/receipt_statistics")
    public void exportReceiptStatistics(HttpServletResponse response) throws IOException {
        List<ReceiptStatistics> receiptStatistics = statementService.getAllReceiptStatistics();

        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Receipt Statistics");

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("序号");
        headerRow.createCell(1).setCellValue("站点名称");
        headerRow.createCell(2).setCellValue("数量");
        headerRow.createCell(3).setCellValue("报表日期");

        // 填充数据行
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int rowNum = 1;
        for (ReceiptStatistics report : receiptStatistics) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(report.getId());
            row.createCell(1).setCellValue(report.getStationName());
            row.createCell(2).setCellValue(report.getQuantity());
            row.createCell(3).setCellValue(formatter.format(report.getReportDate()));
        }

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=receipt_statistics");

        // 写入响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    @GetMapping("/export/dispatch_statistics")
    public void exportDispatchStatistics(HttpServletResponse response) throws IOException {
        List<DispatchStatistics> dispatchStatistics = statementService.getAllDispatchStatistics();

        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Dispatch Statistics");

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("序号");
        headerRow.createCell(1).setCellValue("站点名称");
        headerRow.createCell(2).setCellValue("数量");
        headerRow.createCell(3).setCellValue("报表日期");

        // 填充数据行
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int rowNum = 1;
        for (DispatchStatistics report : dispatchStatistics) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(report.getId());
            row.createCell(1).setCellValue(report.getStationName());
            row.createCell(2).setCellValue(report.getQuantity());
            row.createCell(3).setCellValue(formatter.format(report.getReportDate()));
        }

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=dispatch_statistics");

        // 写入响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }
}
